package com.cqcvc.controller;

import com.cqcvc.pojo.Docs;
import com.cqcvc.service.DocsService;
import com.cqcvc.service.DownLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;

@Controller
public class DocsController {
    @Autowired
    private DocsService docsService;
    @Autowired
    private DownLogService downLogService;

    @RequestMapping("docsSearch")
    public ModelAndView search(@RequestParam(defaultValue = "1") int page,HttpSession session){
        ModelAndView mv=new ModelAndView();
        PageHelper.startPage(page,5);
        String author= (String) session.getAttribute("zh");
        List<Docs> docsList=docsService.search(author);  //查询当前用户的记录
        PageInfo<Docs> pageInfo=new PageInfo<>(docsList);  //创建分页信息
        mv.addObject("list",docsList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("right.jsp");
        return mv;
    }


    @RequestMapping("uploadDocs")
    @ResponseBody
    public String upload(Docs docs, MultipartFile myfile, HttpSession session) throws IOException {
        //1.李四解决附件上传问题
        String originFile=myfile.getOriginalFilename();  //获取附件原始文件名
        String ext=originFile.substring(originFile.lastIndexOf(".")); //获取原始文件的扩展名
        String newFileName=System.currentTimeMillis()+ext;  //利用时间戳来生成新的文件名
        myfile.transferTo(new File("e:/upload/"+newFileName));  //将附件存储到e:/upload目录下，需要事先建好该目录

        //2.将记录信息写入数据库
        String author=(String)session.getAttribute("zh");
        docsService.insert(docs.getTitle(),docs.getType(),newFileName,author);
        //3.给用户提醒信息
        String msg="<script>alert('Upload Success!');location.href='docsSearch'</script>";
        return msg;
    }

    @RequestMapping("docsDown")
    public void downDocs(int id, HttpSession session, HttpServletResponse response) throws IOException {
        //根据id获取记录对象
        Docs docs=docsService.getById(id);
        //附件的地址
        String filePath="e:/upload/"+docs.getUrl();
        //文档标题
        String title=docs.getTitle();
        //下载人
        String author= (String) session.getAttribute("zh");
        //将下载日志写入数据库
        downLogService.insert(title,author);
        //实现文档下载，首先生成数据输入流
        InputStream bis=new BufferedInputStream(new FileInputStream(new File(filePath)));
        //设置response的header
        response.addHeader("Content-Disposition","attachment;filename="+docs.getUrl());
        //设置Content-type
        response.setContentType("multipart/form-data");
        //将输入流转换成输出流
        BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
        int len=0;
        while ((len=bis.read())!=-1){
            out.write(len);
            out.flush();
        }
        out.close();
    }

    @RequestMapping("docsDel")
    public String del(int id){
        //1.删除附件
        Docs docs=docsService.getById(id);
        String filePath="e:/upload/"+docs.getUrl();
        File file=new File(filePath);
        if(file.exists()){
            file.delete();
        }
        //2.根据id删除数据库记录
        docsService.del(id);
        return "docsSearch";
    }
}
